package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.domain.CsSmImportLogD;
import com.example.demo.mapper.CsSmImportLogDMapper;
import com.ffcs.oss.param.out.ServiceResp;
import com.ffcs.oss.param.vm.QueryPageVm;
import com.example.demo.service.ImportLogService;
import com.example.demo.web.rest.evt.CsSmImportLogDEvt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;

@Service
public class ImportLogServiceImpl extends ServiceImpl<CsSmImportLogDMapper, CsSmImportLogD> implements ImportLogService {

    @Autowired
    CsSmImportLogDMapper csSmImportLogDMapper;

    @Override
    public ServiceResp getList(CsSmImportLogDEvt evt) {
        try {
            CsSmImportLogD csSmImportLogD = new CsSmImportLogD();
            BeanUtils.copyProperties(evt, csSmImportLogD);
            LambdaQueryWrapper<CsSmImportLogD> queryWrapper = new LambdaQueryWrapper<>(csSmImportLogD);
            if (evt.getCreatedTimeStart() != null) {
                queryWrapper.ge(CsSmImportLogD::getCreatedTime, evt.getCreatedTimeStart());
            }
            if (evt.getCreatedTimeEnd() != null) {
                queryWrapper.le(CsSmImportLogD::getCreatedTime, evt.getCreatedTimeEnd());
            }
            Page<CsSmImportLogD> page = new Page<CsSmImportLogD>()
                    .setCurrent(evt.getPageNo())
                    .setSize(evt.getPageSize());
            IPage<CsSmImportLogD> result = csSmImportLogDMapper.selectPage(page, queryWrapper
                    .orderByDesc(CsSmImportLogD::getCreatedTime));
            QueryPageVm<Object> queryPageVm = QueryPageVm.getInstance(evt, Collections.singletonList(result.getRecords()), result.getTotal());
            return ServiceResp.getInstance().success(queryPageVm);
        } catch (Exception e) {
            e.printStackTrace();
            return ServiceResp.getInstance().error();
        }
    }
}
